///////////////////////////////////////////////////////////////////////////////
// The Archimeds project 
//
// C++ Implementation: secservconinflation
//
// Description: 
//
//
// Author: Archimedes Projekt <thelsb@gmail.com>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
////////////////////////////////////////////////////////////////////////////////
#include "secservconinflation.h"
#include "dlginflation.h"
#include "qextserialport.h"
#include "dlginflationconfirm.h"
#include <iostream>

SecServConInflation::SecServConInflation(QWidget *parent)
 : QWidget(parent),
 m_serial(NULL)
{
    this->setAutoFillBackground(true);
    ui.setupUi(this);
    QPixmap bgpixmap = QPixmap(":images/modules/servcon/inflation/bkg_inflation.png");
    if(bgpixmap.isNull()) std::cout<<"SecServConInflation: null background pixmap"<<std::endl;
    QPalette p = this->palette();
    p.setBrush(this->backgroundRole(),QBrush(bgpixmap));
    this->setPalette(p);
    
    initControls();
    
    connect(ui.btnManOverride,SIGNAL(clicked()),this,SLOT(manOverride()));
    
}


SecServConInflation::~SecServConInflation()
{
}




/*!
    \fn SecServConInflation::btnUploadSeq()
 */
PStyledButton * SecServConInflation::btnUploadSeq()
{
    return ui.btnUploadSeq;
}


/*!
    \fn SecServConInflation::btnReadSeq()
 */
PStyledButton * SecServConInflation::btnReadSeq()
{
    return ui.btnReadSeq;
}


/*!
    \fn SecServConInflation::btnManOverride()
 */
PStyledButton * SecServConInflation::btnManOverride()
{
    return ui.btnManOverride;
}


/*!
    \fn SecServConInflation::initControls()
 */
void SecServConInflation::initControls()
{
    btnUploadSeq()->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/inflation/btn_upload_seq_pressed.png"));
    btnUploadSeq()->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/inflation/btn_upload_seq_pressed.png"));
    btnUploadSeq()->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/inflation/btn_upload_seq_active.png"));
    
    btnReadSeq()->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/inflation/btn_read_seq_pressed.png"));
    btnReadSeq()->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/inflation/btn_read_seq_pressed.png"));
    btnReadSeq()->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/inflation/btn_read_seq_active.png"));
    
    btnManOverride()->replaceStatePixmap(PStyledButton::off_pressed,QPixmap(":images/modules/servcon/inflation/btn_man_override_pressed.png"));
    btnManOverride()->replaceStatePixmap(PStyledButton::on_pressed,QPixmap(":images/modules/servcon/inflation/btn_man_override_pressed.png"));
    btnManOverride()->replaceStatePixmap(PStyledButton::on,QPixmap(":images/modules/servcon/inflation/btn_man_override_active.png"));
    
    
}


/*!
    \fn SecServConInflation::manOverride()
 */
void SecServConInflation::manOverride()
{
    DlgInflationConfirm dlgConfirm;
    //dlgConfirm.setParent(this);
    dlgConfirm.setWindowFlags(Qt::Window |Qt::FramelessWindowHint );
    dlgConfirm.setWindowModality(Qt::ApplicationModal);
    dlgConfirm.setModal(true);
    dlgConfirm.move(mapToGlobal(this->pos()).x()+75,mapToGlobal(this->pos()).y()+70);
    if(dlgConfirm.exec() == QDialog::Accepted)
    {
        QSettings settings("../../gss.conf",QSettings::IniFormat);  
        QString strPort     = settings.value("Serial_Ports/ServCon/port").toString();
        int iSpeed    = settings.value("Serial_Ports/ServCon/speed").toInt();
        int iDataBits = settings.value("Serial_Ports/ServCon/dataBits").toInt();
        float fStopBits = settings.value("Serial_Ports/ServCon/stopBits").toString().toFloat();
        QString strParity   = settings.value("Serial_Ports/ServCon/parity").toString();
        QString strFlow     = settings.value("Serial_Ports/ServCon/flow").toString();
        
        if(strPort.isEmpty())
        {
            qWarning()<<"could not read 'port' from settings', can't start inlfation";
            return;
        }
        
        qDebug()<<"serial port:"<<strPort;
        if(!m_serial)
            m_serial = new QextSerialPort(strPort);
        if(!m_serial)
        {
            qWarning("Inflation could not allocate serial port object, aborting") ;
            return;
        }
        else {
            m_serial->setBaudRate(intToBaud( iSpeed ));
            m_serial->setDataBits(intToDataBits(iDataBits));
            m_serial->setParity(strToParity(strParity));
            m_serial->setStopBits(floatToStopBits(fStopBits));
            m_serial->setFlowControl(strToFlow(strFlow));
            if(!m_serial->open())
            {
                qWarning()<<"Inflation coulnd not open serial port, aborting "<<strPort;
                return;
            }
            else qWarning()<<"Inflation opened "<<strPort;
        }
        
        DlgInflation dlgInflation;
        dlgInflation.setSerialPort(m_serial);
        dlgInflation.exec();
        m_serial->close();
        delete m_serial;
        m_serial = NULL;
    }
}
